﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2  : System.Web.UI.Page
{
    MyDataDataContext db = new MyDataDataContext();

    public void Delete(string UserId, string GoodsId)
    {
        Collection collection = (from c in db.Collection
                                 where c.UserID == UserId && c.SaleOfGoodsID == GoodsId
                                 select c).First();
        if(collection != null)
        {
            db.Collection.DeleteOnSubmit(collection);
            db.SubmitChanges();
        }

    }
    public void Clear(string UserId)
    {
        List<Collection> collectionList= (from c in db.Collection
                                          where c.UserID == UserId 
                                          select c).ToList();
        foreach (Collection collection in collectionList)
        {
            db.Collection.DeleteOnSubmit(collection);

        }
        db.SubmitChanges();
    }
    
   
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            if (Session["UserID"] == null)//用户未登录
            {
                Response.Redirect("~/Login.aspx");
            }
            Bind();
        }
    }

    protected void Bind()
    {
        var result = from r in db.Collection
                    where r.UserID == Convert.ToString(Session["UserID"])
                    select new
                    {
                        r.SaleOfGoodsID,
                        r.CollectionTime,
                        r.GoodsName
                    };//映射需要显示的字段
        gvCart.DataSource = result;
        gvCart.DataBind();

        if (gvCart.Rows.Count != 0)
        {
            pnlCart.Visible = true;
            lblCart.Text = ""; 
        }
        else
        {
            pnlCart.Visible = false;
            lblCart.Text = "我的收藏内无商品，请先收藏！";
        }
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow gv in gvCart.Rows)//对应每一行记录
        {
            string GoodsId = gv.Cells[1].Text;//获取GoodsID用于删除

            CheckBox chkProduct = new CheckBox();
            chkProduct = (CheckBox)gv.FindControl("chkProduct");
            if (chkProduct != null)
            {
                if (chkProduct.Checked)
                {                    
                    Delete(Convert.ToString(Session["UserID"]), GoodsId); //删除收藏车中指定商品编号的商品
                }
            }
        }


        //弹出删除单个收藏成功提示
        Response.Write("<script language='javascript'>");
        Response.Write("alert('删除收藏成功！');");
        Response.Write("</script>");
        Bind();  //调用自定义方法Bind()显示购物车中商品        
    }


    protected void btnClear_Click(object sender, EventArgs e)
    {
        Clear(Convert.ToString(Session["UserID"]));        
        Response.Write("<script language='javascript'>");
        Response.Write("alert('清空收藏成功！');");
        Response.Write("</script>");
    }
   }